<?php

// 载入配置文件
require_once '../../config.php';

//先给用户一个箱子
session_start();

    function login()
    {
        if (empty($_POST['email'])) {
            $GLOBALS['message'] = '请检查用户名';
            return;
        }
        if (empty($_POST['password'])) {
            $GLOBALS['message'] = '密码错误';
            return;
        }
        $email = $_POST['email'];
        $password = $_POST['password'];


        // 数据库的操作
        $conn = mysqli_connect(XIU_DB_HOST, XIU_DB_USER, XIU_DB_PASS, XIU_DB_NAME);
        if (!$conn) {
            exit('<h1>数据库连接失败</h1>');
        };

        $query = mysqli_query($conn, "SELECT * FROM users where email = '{$email}' limit 1;");


        if (!$query) {
            $GLOBALS['message'] = '登录失败，请重试';
            return;
        };
        $user = mysqli_fetch_assoc($query);
   
        if (!$user) {
            $GLOBALS['message'] = '邮箱与密码不匹配';
            return;
        }
        if ($user['password'] !== $password) {
            $GLOBALS['message'] = '邮箱与密码不匹配';
            return;
        }



        $_SESSION['current_login_user'] = $user;
        if ($user['guanli']=="1") {
            header('Location: /admin/index.php');
        }
        if ($user['guanli']=="2") {
            header('Location: /admin/users/index.php');
        }
    }
if ($_SERVER['REQUEST_METHOD'] ==='POST') {
    login();
}

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sign in &laquo; User</title>
    <link rel="stylesheet" href="/static/assets/vendors/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="/static/assets/css/login_css.css">
    <script src="/static/assets/vendors/jquery/jquery.min.js"></script>
      
</head>
<body>
    <div id="login" class="">
        <form class="login_in" action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
            <img class="avatar" src="/static/assets/img/back.jpg">
            <?php if (isset($message)) : ?>
            <div class="alert alert-danger warn">
              <?php echo $message; ?>
            </div>
            <?php endif; ?>
                <label for="email" class="sr-only">邮箱</label>
                <input id="email" name="email" type="email" class="form-control" placeholder="邮箱" autofocus value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>">

                <label for="password" class="sr-only">密码</label>
                <input id="password" name="password" type="password" class="form-control" placeholder="密码">
              <button class="btn btn-primary btn-block">登 录</button>
              <span class="regist_account">你想要一个账户<a href="register.php">注册</a></span>
        </form>
    </div>
    <script>
        
            $(function($){

                //时机： 输入邮箱过后，页面展示对应的头像
                // 实现：
                    // 时机：邮箱失去焦点
                    // 事情：获取头像地址，展示到 img 元素上

                    var emailFormat = /^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]+$/

                    $('#email').on('blur',function(){
                       var value = $(this).val()
                    //    忽略掉空文本，或者不是一个邮箱
                       if(!value || !emailFormat.test(value))  return;

                    //    输入正确，获取地址，展示到 img 元素上，js 发送 ajax 获取

                    $.get('/admin/api/avatar.php',{ email: value },function(res){
                        if(!res)   return
                        // 展示元素
                        console.log(res)
                        $('.avatar').fadeOut(function(){
                            $(this).on('load',function(){
                                $(this).fadeIn();
                            }).attr('src',res);

                        })

                    })

                })
            })
        // var kk = document.getElementById("#login").style.background
    </script>
</body>

</html>